<template>
  <el-dialog
    title="设备绑定的openid"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="openId"
        header-align="center"
        align="center"
        >
      </el-table-column>

      <el-table-column
      fixed="right"
      header-align="center"
      align="center"
      width="150"
      label="操作">
      <template slot-scope="scope">
      <el-button type="text" size="small" @click="forceUnbind(scope.row.openId)">强制解绑</el-button>
      <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
      </template>
      </el-table-column>
    </el-table>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <!--<el-button type="primary" @click="dataFormSubmit()">确定</el-button>-->
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        mac: ''
      }
    },
    methods: {
      forceUnbind (openId) {
        this.$http({
          url: this.$http.adornUrl(`/generator/twechatdevice/getDeviceUnBindOpenId`),
          method: 'get',
          params: this.$http.adornParams({
            mac: this.mac,
            openId: openId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.list
          }
        })
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      init (mac) {
        this.mac = mac || 0
        this.visible = true
        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()

          this.$http({
            url: this.$http.adornUrl(`/generator/twechatdevice/getDeviceBindOpenId`),
            method: 'get',
            params: this.$http.adornParams({
              mac: mac
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataList = data.list
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.deviceMac.length !== 12) {
          this.$message.error('长度固定12位')
          return
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/twechatdevice/deviceAuth`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deviceId': this.dataForm.deviceId,
                'createTime': this.dataForm.createTime,
                'deviceMac': this.dataForm.deviceMac,
                'flag': this.dataForm.flag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
